<template>
     <div>
            <template v-if="dataList.length > 0">
                <div
                  v-for="(item, index) in dataList"
                  class="vilo-check"
                  :class="{ checked: checkbox[item] }"
                  :key="index"
                  @click="viloCheck(item)"
                >
                  <div class="left">
                    <span>{{ item }}</span>
                  </div>
                  <span v-if="checkbox[item]" class="iconfont icon-check1"></span>
                  <span v-else class="iconfont icon-check"></span>
                </div>
            <p>选项列表: {{checkedList}}</p>
            </template>
            
          </div>
</template>

<script>
export default {
  data() {
    return {
      
    dataList:['apple','banana','orange' ],
      checkbox: {},
      // checkedList:[]
    }
  },
  computed:{
    checkedList() {
      let list = []
        for (const key in this.checkbox) {
        if (this.checkbox[key]) {
          list.push(key);
        }
      }
      return list
    }
  },
  methods:{
    getData() {
       let obj = [];
        this.dataList.forEach((item) => {
          obj[item] = true;
        });
        this.checkbox = { ...obj };
    },
       viloCheck(name) {
      //   this.checkbox[name] = !this.checkbox[name];
      //  this.checkbox = {...this.checkbox}
      this.checkbox = Object.assign({}, this.checkbox, {
        [name]: !this.checkbox[name],
      });
      // console.log(this.checkbox);
    },
    // getChecked() {
    //    for (const key in this.checkbox) {
    //     if (this.checkbox[key]) {
    //       this.checkedList.push(key);
    //     }
    //   }
    // }
  }
}
</script>

<style scoped>
.vilo-check {
  width: 360px;
  height: 84px;
  border: 1pt solid #b7b7b7;
  display: flex;
  justify-content: space-around;
  align-items: center;
  border-radius: 10pt;
  margin-bottom: 16px;
  cursor: pointer;
}
.vilo-check img {
  margin-right: 20px;
}
.vilo-check>span {
  color: var(--el-color-primary);
}
.vilo-check:hover {
  border-color: var(--el-color-primary);
}
.vilo-check.checked {
  border-color: var(--el-color-primary);
  border-width: 1pt;
}
.vilo-check.checked span{
  color:var(--el-color-primary);
}
</style>